/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.checkinfos.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.service.CrudService;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.modules.checkinfos.entity.Checkinfos;
import com.fosung.frame.modules.sys.utils.UserUtils;
import com.fosung.frame.modules.approve.entity.HomeApprove;
import com.fosung.frame.modules.checkinfos.dao.CheckinfosDao;

/**
 * 审核记录Service
 * @author zhangfn
 * @version 2017-08-30
 */
@Service
@Transactional(readOnly = true)
public class CheckinfosService extends CrudService<CheckinfosDao, Checkinfos> {

	public Checkinfos get(String id) {
		return super.get(id);
	}
	
	public List<Checkinfos> findList(Checkinfos checkinfos) {
		return super.findList(checkinfos);
	}
	
	public Page<Checkinfos> findPage(Page<Checkinfos> page, Checkinfos checkinfos) {
		return super.findPage(page, checkinfos);
	}
	
	/**
	 * 保存审核组织生活计划意见
	 * @param checkinfos
	 */
	@Transactional(readOnly = false)
	public void saveShjhyj(Checkinfos checkinfos) {
		String approveContent = checkinfos.getApproveContent();
		if(StringUtils.isEmpty(approveContent)) {
			String approveStatus = checkinfos.getApproveStatus();
			if("3".equals(approveStatus)) {
				checkinfos.setApproveContent("同意");
			}else if("4".equals(approveStatus)) {
				checkinfos.setApproveContent("退回");
			}else if("5".equals(approveStatus)) {
				checkinfos.setApproveContent("拒绝");
			}
		}
		super.save(checkinfos);
	}
	
	/**
	 * 保存审核积分意见
	 * @param checkinfos
	 */
	@Transactional(readOnly = false)
	public void saveShjfyj(Checkinfos checkinfos) {
		String approveContent = checkinfos.getApproveContent();
		if(StringUtils.isEmpty(approveContent)) {
			String approveStatus = checkinfos.getApproveStatus();
			if("3".equals(approveStatus)) {
				checkinfos.setApproveContent("同意");
			}else if("4".equals(approveStatus)) {
				checkinfos.setApproveContent("拒绝");
			}else if("5".equals(approveStatus)) {
				checkinfos.setApproveContent("退回");
			}
		}
		super.save(checkinfos);
	}
	
	@Transactional(readOnly = false)
	public void delete(Checkinfos checkinfos) {
		super.delete(checkinfos);
	}
	
	public Checkinfos findCheckinfosByOne(HomeApprove homeApprove) {
		Checkinfos ci = new Checkinfos();
		ci.setOrganizeLifeId(homeApprove.getId());
		List<Checkinfos> listCheckinfos = findList(ci);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String userId = UserUtils.getUser().getId();
		if (listCheckinfos.size() > 0) {
			if (listCheckinfos.get(0).getApproveStatus().equals(homeApprove.getShstate())) {
				ci = listCheckinfos.get(0);
				ci.setShTime(format.format(ci.getCreateDate()));
			}else {
				ci.setApproveUserId(userId);
				ci.setShTime(format.format(new Date()));
			}
		} else {
			ci.setApproveUserId(userId);
			ci.setShTime(format.format(new Date()));
		}
		return ci;
	}
	
}